package com.lepai.common;

/**
 * @author weihaolong
 * @date 2018/11/06 19:49
 */
public class Test {

    /**
     * 霍纳法则
     * @param arr
     * @param x
     * @param n
     * @return
     */
    public static long poly2(int[] arr, int x, int n){
        //arr存储系数, x 表示基数, n 表示幂
        long poly = 0;
        for(int i = n; i >= 0; i--)
            poly = poly * x + arr[i];
        return poly;
    }

    /**
     * 朴素求和方法
     * @param arr
     * @param n
     * @param x
     */
    public static long poly(int[] arr, int x, int n){
        long poly = 0;
        for(int i = n; n >= 0; n--){
            long temp = arr[n];
            for(int j = n; j > 0; j--){
                temp *= x;
            }
            poly += temp;
        }
        return poly;
    }

    public static void main(String[] args) {
        int[]b =  {2,1,0,8,4};
        System.out.println(poly2(b,3,4));
        System.out.println(poly(b,3,4));
    }
}
